#include "businessfactory.h"

Business *BusinessFactory::createBusiness(CWHttpRequest &request)
{
    Type type = getEnumType(request);
    switch (type)
    {
    case Type::kLoginRequest:
        return new LoginBusiness;
        break;
    case Type::kSignupRequest:
        return new SignupBusiness;
        break;
    case Type::kErrorRequest:
        return nullptr;
        break;
    default:
        return nullptr;
        break;
    }
}

Type BusinessFactory::getEnumType(CWHttpRequest &request)
{
    // 获取请求方法
    std::string request_method = request.getRequestMethod();
    // 获取请求URL
    std::string request_URL = request.getRequestURL();
    // 获取请求体数据解析格式
    std::string request_type = request.getRequestHeader("Content-Type");
    
    if (request_method == "POST")
    {
        if (request_URL == "/login" && request_type.find("application/json") != std::string::npos)
            return Type::kLoginRequest;
        else if (request_URL == "/signup" && request_type == "application/json")
            return Type::kSignupRequest;
    }
    else if (request_method == "GET")
        return Type::kErrorRequest;
    return Type::kErrorRequest;
}